package com.xhb.biz.service;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.solon.service.impl.ServiceImpl;
import com.xhb.biz.feign.UserDataSyncService;
import com.xhb.biz.mapper.ProjectUrlMapper;
import com.xhb.biz.po.ProjectUrlPO;
import org.noear.solon.aspect.annotation.Service;
import org.noear.solon.data.annotation.Tran;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * (Project)}表服务接口
 *
 * @author tanyukun
 * @since 2023-02-06 13:44:04
 */
@Service
public class ProjectUrlService extends ServiceImpl<ProjectUrlMapper, ProjectUrlPO> {

    /**
     * 首页导航分组同步
     *
     * @param userService feign 接口
     * @param headers     请求头
     */
    @Tran
    public void projectUrlListSync(UserDataSyncService userService, Map<String, Object> headers) {
        String data = userService.getProjectCollectList(headers);
        List<ProjectUrlPO> projectUrlPos = JSONUtil.toList(data, ProjectUrlPO.class);
        if (CollectionUtil.isNotEmpty(projectUrlPos)) {
            super.saveOrUpdateBatch(projectUrlPos);
            // 删除已被管理端删除的旧数据
            LambdaQueryWrapper<ProjectUrlPO> delWrapper = new LambdaQueryWrapper<>();
            delWrapper.notIn(ProjectUrlPO::getId, projectUrlPos.stream().map(ProjectUrlPO::getId).collect(Collectors.toList()));
            super.remove(delWrapper);
        }
    }


}
